package view;

import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JPanel;
import model.Auktion;
import model.Kategori;

/**
 *
 * @author Jeppe
 */
public class Auktionsliste extends javax.swing.JPanel {

    // COMBO MODELLER MØBLER
    public static final String[] MOBLER_NAVN = new String[]{"Alle", "Spisebord", "Servante", "TV-bord", "Ægget"};
    public static final String[] MOBLER_DIMENSIONER = new String[]{"Alle", "50x75x110", "1500x2000x80", "2000x2000x2000"};
    public static final String[] MOBLER_MATERIALE = new String[]{"Alle", "Læder", "Teak", "Plast"};
    // COMBO MODELLER MALERI 
    public static final String[] MALERI_KUNSTNER = new String[]{"Alle", "Claude Monet", "PIQ ass O"};
    public static final String[] MALERI_AARSTAL = new String[]{"Alle", "Gotisk", "Abstract", "Forvirrende", "lad være næste gang"};
    public static final String[] MALERI_STILART = new String[]{"Alle", "1847", "1900"};
    public static final String[] MALERI_STOERRELSE = new String[]{"Alle", "60x120", "80x250" };
    
    // COMBO MODELLER SMYKKER
    public static final String[] SMYKKER_EDELSTEN = new String[]{"Alle", "Ja", "Nej" };
    public static final String[] SMYKKER_STEMPEL = new String[]{"Alle", "Ja", "Nej" };
     
    // COMBO MODELLER VIN
    public static final String[] VIN_PRODUCENT = new String[]{"Alle", "Domaine Romanée-Conti", "Clausade", "Alsace", "Rondo" };
    public static final String[] VIN_LAND = new String[]{"Alle", "Italien", "Spanien", "Frankrig", "California" };
    public static final String[] VIN_DRUER = new String[]{"Alle", "Rødvin", "Hvidvin", "Rosavin", "portvin" };
    public static final String[] VIN_AARGANG = new String[]{"Alle", "1978", "1999", "2000", "2001", "2004" };
    
    private MainGui mainGui;
    private ArrayList<JPanel> panelListe;
    private boolean startUp;

    private void closeAllSubCombo() {
        jComboBox_sorting1.setVisible(false);
        jComboBox_sorting2.setVisible(false);
        jComboBox_sorting3.setVisible(false);
        jComboBox_sorting4.setVisible(false);
        jLabel_sorting1.setVisible(false);
        jLabel_sorting2.setVisible(false);
        jLabel_sorting3.setVisible(false);
        jLabel_sorting4.setVisible(false);
    }


    public Auktionsliste(MainGui main) {
        initComponents();
        closeAllSubCombo();
        this.mainGui = main;
        startUp = true;
        kategoriCombo.removeAllItems();
        for (int i = 0; i < mainGui.getAuktionsHandler().getKategorier().size(); i++) {
            kategoriCombo.addItem(mainGui.getAuktionsHandler().getKategorier().get(i));
        }
        visAuktionsPaneler(new Kategori("Vis alle"));
        startUp = false;
    }
    
    private void visAuktionsPaneler(Kategori enKategori) {
        panelListe = new ArrayList<>();
        if (enKategori.getNavn().equals("Vis alle")) {
            for (int i = 0; i < mainGui.getAuktionsHandler().getAuktioner().size(); i++) {
                panelListe.add(new AuktionslistePanel(mainGui, mainGui.getAuktionsHandler().getAuktioner().get(i)));
            }
        }
        if (enKategori.getNavn().equals("Vis aktive")) {
            for (int i = 0; i < mainGui.getAuktionsHandler().getAktiveAuktioner().size(); i++) {
                panelListe.add(new AuktionslistePanel(mainGui, mainGui.getAuktionsHandler().getAktiveAuktioner().get(i)));
            }
        } else {

            // hent liste af auktioner når man har valgt søge term og efter kategori
            String item =  (String)jComboBox_sorting1.getSelectedItem();
            String item2 = (String)jComboBox_sorting2.getSelectedItem();
            String item3 = (String)jComboBox_sorting3.getSelectedItem();
            String item4 = (String)jComboBox_sorting4.getSelectedItem();
            
            ArrayList<Auktion> liste = mainGui.getAuktionsHandler().getAuktionsListBySearch(
                                                                enKategori.getNavn(), 
                                                                item, item2, item3, item4);                                                               
                for (int i = 0; i < liste.size(); i++) {
                    panelListe.add(new AuktionslistePanel(mainGui, liste.get(i)));
                }
        }
        listePanel.removeAll();
        int y = 10;
        for (int i = 0; i < panelListe.size(); i++) {
            panelListe.get(i).setBounds(7, y, 589, 120);
            panelListe.get(i).setVisible(true);
            listePanel.add(panelListe.get(i));
            y = y + 130;
        }

        listePanel.setPreferredSize(new Dimension(890, y));
        listePanel.repaint();
    }
    
    public void opdaterSubCombos(String kategori ) {
        // choose sub search menu after category
        closeAllSubCombo();
        switch (kategori) {
            case "Møbel":                
                jLabel_sorting1.setText("Type");
                jComboBox_sorting1.setModel( new javax.swing.DefaultComboBoxModel( MOBLER_NAVN ) );
                jLabel_sorting2.setText("Dimension");
                jComboBox_sorting2.setModel( new javax.swing.DefaultComboBoxModel(MOBLER_DIMENSIONER ) );
                jLabel_sorting3.setText("Navn");
                jComboBox_sorting3.setModel( new javax.swing.DefaultComboBoxModel( MOBLER_MATERIALE ) );
                jComboBox_sorting1.setVisible(true);
                jComboBox_sorting2.setVisible(true);
                jComboBox_sorting3.setVisible(true);
                jLabel_sorting1.setVisible(true);
                jLabel_sorting2.setVisible(true);
                jLabel_sorting3.setVisible(true);
                break;
            case "Maleri":
                jLabel_sorting1.setText("Kunstner");
                jComboBox_sorting1.setModel( new javax.swing.DefaultComboBoxModel( MALERI_KUNSTNER ) );
                jLabel_sorting2.setText("Årstal");
                jComboBox_sorting2.setModel( new javax.swing.DefaultComboBoxModel( MALERI_AARSTAL ) );
                jLabel_sorting3.setText("Stilart");
                jComboBox_sorting3.setModel( new javax.swing.DefaultComboBoxModel( MALERI_STILART ) );
                jLabel_sorting4.setText("Størrelse");
                jComboBox_sorting4.setModel( new javax.swing.DefaultComboBoxModel( MALERI_STOERRELSE ) );
                jComboBox_sorting1.setVisible(true);
                jComboBox_sorting2.setVisible(true);
                jComboBox_sorting3.setVisible(true);                
                jComboBox_sorting4.setVisible(true);     
                jLabel_sorting1.setVisible(true);
                jLabel_sorting2.setVisible(true);
                jLabel_sorting3.setVisible(true);
                jLabel_sorting4.setVisible(true);
                break;
            case "Smykke":
                jLabel_sorting1.setText("Ædelsten");
                jComboBox_sorting1.setModel( new javax.swing.DefaultComboBoxModel( SMYKKER_EDELSTEN ) );
                jLabel_sorting2.setText("Stempel");
                jComboBox_sorting2.setModel( new javax.swing.DefaultComboBoxModel( SMYKKER_STEMPEL ) );
                jComboBox_sorting1.setVisible(true);
                jComboBox_sorting2.setVisible(true);
                jLabel_sorting1.setVisible(true);
                jLabel_sorting2.setVisible(true);
                break;
            case "Vin":
                jLabel_sorting1.setText("Producent");
                jComboBox_sorting1.setModel( new javax.swing.DefaultComboBoxModel( VIN_PRODUCENT ) );
                jLabel_sorting2.setText("Land");
                jComboBox_sorting2.setModel( new javax.swing.DefaultComboBoxModel( VIN_LAND ) );
                jLabel_sorting3.setText("Druer");
                jComboBox_sorting3.setModel( new javax.swing.DefaultComboBoxModel( VIN_DRUER ) );
                jLabel_sorting4.setText("Årgang");
                jComboBox_sorting4.setModel( new javax.swing.DefaultComboBoxModel( VIN_AARGANG ) );
                jComboBox_sorting1.setVisible(true);
                jComboBox_sorting2.setVisible(true);
                jComboBox_sorting3.setVisible(true);                
                jComboBox_sorting4.setVisible(true);     
                jLabel_sorting1.setVisible(true);
                jLabel_sorting2.setVisible(true);
                jLabel_sorting3.setVisible(true);
                jLabel_sorting4.setVisible(true);                
                break;
        }
        repaint();
        revalidate();
    }
    
    

    public void opdater() {
//        System.out.println("en kategori " + kategoriCombo.getSelectedItem());
        visAuktionsPaneler((Kategori) kategoriCombo.getSelectedItem());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        kategoriCombo = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        listePanel = new javax.swing.JPanel();
        jComboBox_sorting1 = new javax.swing.JComboBox();
        jComboBox_sorting2 = new javax.swing.JComboBox();
        jComboBox_sorting3 = new javax.swing.JComboBox();
        jComboBox_sorting4 = new javax.swing.JComboBox();
        jLabel_sorting1 = new javax.swing.JLabel();
        jLabel_sorting2 = new javax.swing.JLabel();
        jLabel_sorting3 = new javax.swing.JLabel();
        jLabel_sorting4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setOpaque(false);
        setLayout(null);

        kategoriCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                kategoriComboItemStateChanged(evt);
            }
        });
        kategoriCombo.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                kategoriComboPropertyChange(evt);
            }
        });
        add(kategoriCombo);
        kategoriCombo.setBounds(20, 20, 153, 22);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        listePanel.setBackground(new java.awt.Color(204, 204, 204));
        listePanel.setOpaque(false);

        javax.swing.GroupLayout listePanelLayout = new javax.swing.GroupLayout(listePanel);
        listePanel.setLayout(listePanelLayout);
        listePanelLayout.setHorizontalGroup(
            listePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 890, Short.MAX_VALUE)
        );
        listePanelLayout.setVerticalGroup(
            listePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 563, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(listePanel);

        add(jScrollPane1);
        jScrollPane1.setBounds(270, 20, 606, 508);

        jComboBox_sorting1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox_sorting1ItemStateChanged(evt);
            }
        });
        add(jComboBox_sorting1);
        jComboBox_sorting1.setBounds(20, 83, 153, 22);

        jComboBox_sorting2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox_sorting2ItemStateChanged(evt);
            }
        });
        add(jComboBox_sorting2);
        jComboBox_sorting2.setBounds(20, 145, 153, 22);

        jComboBox_sorting3.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox_sorting3ItemStateChanged(evt);
            }
        });
        add(jComboBox_sorting3);
        jComboBox_sorting3.setBounds(20, 201, 153, 22);

        jComboBox_sorting4.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox_sorting4ItemStateChanged(evt);
            }
        });
        add(jComboBox_sorting4);
        jComboBox_sorting4.setBounds(20, 256, 153, 22);

        jLabel_sorting1.setText("jLabel1");
        add(jLabel_sorting1);
        jLabel_sorting1.setBounds(20, 58, 153, 19);

        jLabel_sorting2.setText("jLabel1");
        add(jLabel_sorting2);
        jLabel_sorting2.setBounds(20, 120, 153, 16);

        jLabel_sorting3.setText("jLabel1");
        add(jLabel_sorting3);
        jLabel_sorting3.setBounds(20, 183, 153, 16);

        jLabel_sorting4.setText("jLabel1");
        add(jLabel_sorting4);
        jLabel_sorting4.setBounds(20, 239, 153, 16);

        jLabel1.setText("Kategori:");
        add(jLabel1);
        jLabel1.setBounds(20, 0, 150, 16);
    }// </editor-fold>//GEN-END:initComponents

    private void kategoriComboPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_kategoriComboPropertyChange
//        visAuktionsPaneler((Kategori)kategoriCombo.getSelectedItem());
    }//GEN-LAST:event_kategoriComboPropertyChange

    private void kategoriComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_kategoriComboItemStateChanged
        if (!startUp) {
            Kategori enKategori = (Kategori)kategoriCombo.getSelectedItem();
            opdaterSubCombos( enKategori.getNavn() );
            visAuktionsPaneler((Kategori) kategoriCombo.getSelectedItem());
        }
    }//GEN-LAST:event_kategoriComboItemStateChanged

    private void jComboBox_sorting1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox_sorting1ItemStateChanged
        if (!startUp) {
            visAuktionsPaneler((Kategori) kategoriCombo.getSelectedItem());
        }
    }//GEN-LAST:event_jComboBox_sorting1ItemStateChanged

    private void jComboBox_sorting2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox_sorting2ItemStateChanged
         if (!startUp) {
            visAuktionsPaneler((Kategori) kategoriCombo.getSelectedItem());
        }
    }//GEN-LAST:event_jComboBox_sorting2ItemStateChanged

    private void jComboBox_sorting3ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox_sorting3ItemStateChanged
             if (!startUp) {
            visAuktionsPaneler((Kategori) kategoriCombo.getSelectedItem());
        }

    }//GEN-LAST:event_jComboBox_sorting3ItemStateChanged

    private void jComboBox_sorting4ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox_sorting4ItemStateChanged
        if (!startUp) {
            visAuktionsPaneler((Kategori) kategoriCombo.getSelectedItem());
        }

    }//GEN-LAST:event_jComboBox_sorting4ItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox_sorting1;
    private javax.swing.JComboBox jComboBox_sorting2;
    private javax.swing.JComboBox jComboBox_sorting3;
    private javax.swing.JComboBox jComboBox_sorting4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel_sorting1;
    private javax.swing.JLabel jLabel_sorting2;
    private javax.swing.JLabel jLabel_sorting3;
    private javax.swing.JLabel jLabel_sorting4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox kategoriCombo;
    private javax.swing.JPanel listePanel;
    // End of variables declaration//GEN-END:variables
}
